package cn.myframe.shiro.config;

import cn.myframe.shiro.IgnoreUrlUtils;
import cn.myframe.shiro.JWTFilter;
import cn.myframe.shiro.MyRealm;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * shiro配置
 * 
 * @author  ynz
 * @email   ynz@myframe.cn
 * @version 创建时间：2018年6月13日 上午11:31:43
 */
@Configuration
public class ShiroConfig {

    @Bean("securityManager")
    public DefaultWebSecurityManager getManager(MyRealm realm) {
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        // 使用自己的realm
        manager.setRealm(realm);
        DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
        //因为完全实现无状态鉴权,禁用Session
        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
        defaultSessionStorageEvaluator.setSessionStorageEnabled(false);
        subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
        manager.setSubjectDAO(subjectDAO);
        return manager;
    }

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean factory(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        factoryBean.setLoginUrl("/login.html");
        factoryBean.setUnauthorizedUrl("/login.html");
        // 添加自己的过滤器并且取名为jwt
        Map<String, Filter> filterMap = new HashMap<>();
        filterMap.put("jwt", new JWTFilter());
        factoryBean.setFilters(filterMap);

        factoryBean.setSecurityManager(securityManager);
        factoryBean.setUnauthorizedUrl("/401");

        
        Map<String, String> filterRuleMap = new LinkedHashMap<>();
        //查询cn.rojao忽略认证的url
        List<String> urlList = IgnoreUrlUtils.findIgnoreUrl("cn/rojao");
        urlList.forEach(url ->{
        	filterRuleMap.put(url, "anon");
        });
        filterRuleMap.put("/sys/login", "anon");
        filterRuleMap.put("/captcha.jpg", "anon");
        filterRuleMap.put("/sys/sysbasedata/getAllBaseData", "anon");
        filterRuleMap.put("/sys/syslog/downloadLogDocument/*", "anon");
        filterRuleMap.put("/swagger**", "anon");
        filterRuleMap.put("/websocket**", "anon");
        filterRuleMap.put("/downExample**", "anon");
        filterRuleMap.put("/rpt/rptuserposition/downExample", "anon");
        filterRuleMap.put("/websocket/info**", "anon");

        filterRuleMap.put("/login.html", "anon");
        filterRuleMap.put("/index.html", "anon");
        filterRuleMap.put("/public/**", "anon");
        filterRuleMap.put("/bus/js/**", "anon");
        filterRuleMap.put("/bus/*.html", "anon");
        filterRuleMap.put("/sys/js/**", "anon");
        filterRuleMap.put("/sys/*.html", "anon");
        filterRuleMap.put("/dialog/**", "anon");
        filterRuleMap.put("/material-pro4.1/**", "anon");

        filterRuleMap.put("/sys/generator/codeDetail", "anon");

        
        // 所有请求通过我们自己的JWT Filter
        filterRuleMap.put("/**", "jwt");
        //filterRuleMap.put("/**", "anon");
        // 访问401和404页面不通过我们的Filter
       // filterRuleMap.put("/401", "anon");
        factoryBean.setFilterChainDefinitionMap(filterRuleMap);
        return factoryBean;
    }

    /**
     * 下面的代码是添加注解支持
     */
    @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        // 强制使用cglib，防止重复代理和可能引起代理出错的问题
        // https://zhuanlan.zhihu.com/p/29161098
        defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
        return defaultAdvisorAutoProxyCreator;
    }

    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }
}
